package brave;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.WorldCreator;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Brave extends JavaPlugin
{
	public static Brave instance;
	public final static String ORIGIN_WORLD = "world";
	public final static String WALLS_0 = "walls0";
	public final static String WALLS_1 = "walls1";
	public final static String WALLS_2 = "walls2";
	public final static String WALLS_3 = "walls3";

	public static Integer NUM_WALL_WORLDS = 3;
	public static HashMap<String, IWallSign> wallSigns = new HashMap<String, IWallSign>();

	private static WallGame[] wallGames = new WallGame[4];
	private static HashMap<String, WallPlayer> wallPlayers = new HashMap<String, WallPlayer>();

	public static Location origin_spawn_location;
	private static final Location[][] teamLocations = new Location[4][3];
	
	public void onEnable()
	{
		instance = this;
		setUp();

		getLogger().info("Your plugin has been enabled!");

		// register all the listeners
		getServer().getPluginManager().registerEvents(new PlayerListener(), this);
		getServer().getPluginManager().registerEvents(new BlockListener(), this);
		getServer().getPluginManager().registerEvents(new InteractListener(), this);
		getServer().getPluginManager().registerEvents(new ChatListener(), this);
		
	}

	private void setUp()
	{
		wallGames[0] = new WallGame(getServer().createWorld(new WorldCreator(WALLS_0)), 1, 0);
		wallGames[1] = new WallGame(getServer().createWorld(new WorldCreator(WALLS_1)), 20, 1);
		wallGames[2] = new WallGame(getServer().createWorld(new WorldCreator(WALLS_2)), 30, 2);
		wallGames[3] = new WallGame(getServer().createWorld(new WorldCreator(WALLS_3)), 45, 3);
		
		System.out.println(getServer().getWorld(ORIGIN_WORLD).getBlockAt(22, 71, 310).getLocation().toString());
		wallSigns.put(getServer().getWorld(ORIGIN_WORLD).getBlockAt(-75, 72, 253).getLocation().toString(), new TeamWallSign(0, 0));
		wallSigns.put(getServer().getWorld(ORIGIN_WORLD).getBlockAt(-75, 72, 251).getLocation().toString(), new TeamWallSign(0, 1));
		wallSigns.put(getServer().getWorld(ORIGIN_WORLD).getBlockAt(-75, 70, 251).getLocation().toString(), new TeamWallSign(0, 2));
		wallSigns.put(getServer().getWorld(ORIGIN_WORLD).getBlockAt(-75, 70, 253).getLocation().toString(), new TeamWallSign(0, 3));
		
		
		wallSigns.put(getServer().getWorld(ORIGIN_WORLD).getBlockAt(-75, 71, 252).getLocation().toString(), new ObserverWallSign(0));
		
		// walls0 -> home
		wallSigns.put(getServer().getWorld(WALLS_0).getBlockAt(348, 146, -793).getLocation().toString(), new ToOriginWallSign());

		// team locations
		Brave.teamLocations[0][0] = new Location(null, 410.5, 115, -729.5);
		Brave.teamLocations[0][1] = new Location(null, 410.5, 115, -736.5);
		Brave.teamLocations[0][2] = new Location(null, 403.5, 115, -729.5);

		Brave.teamLocations[1][0] = new Location(null, 284.5, 115, -729.5);
		Brave.teamLocations[1][1] = new Location(null, 291.5, 115, -729.5);
		Brave.teamLocations[1][2] = new Location(null, 284.5, 115, -736.5);
		
		Brave.teamLocations[2][0] = new Location(null, 284.5, 115, -855.5);
		Brave.teamLocations[2][1] = new Location(null, 284.5, 115, -848.5);
		Brave.teamLocations[2][2] = new Location(null, 291.5, 115, -855.5);

		Brave.teamLocations[3][0] = new Location(null, 410.5, 115, -855.5);
		Brave.teamLocations[3][1] = new Location(null, 403.5, 115, -855.5);
		Brave.teamLocations[3][2] = new Location(null, 410.5, 115, -848.5);
		
		Brave.origin_spawn_location = new Location(getServer().getWorld(Brave.ORIGIN_WORLD), -68.5, 71.5, 252.5);
	}

	public void onDisable()
	{
		getLogger().info("Your plugin has been disabled.");
		getServer().getScheduler().cancelTasks(this);
	}

	public static WallPlayer getWallPlayer(Player player)
	{
		String playerName = player.getName();
		WallPlayer wallPlayer = Brave.wallPlayers.get(playerName);
		
		// never added player yet so create new player
		if (wallPlayer == null)
		{
			wallPlayer = new WallPlayer(playerName, player);
			Brave.wallPlayers.put(playerName, wallPlayer);
		}
		
		return wallPlayer;
	}

	public static WallGame getWallGame(Integer gameId)
	{
		return Brave.wallGames[gameId];
	}

	public static Location getTeamLocation(Integer gameId, Integer teamId, Integer teamSlot)
	{
		Location location = Brave.teamLocations[teamId][teamSlot];
		location.setWorld(Brave.wallGames[gameId].getBukkitWorld());
		return location;
	}

	public static Location getObserverLocation(Integer gameId)
	{
		return new Location(Brave.wallGames[gameId].getBukkitWorld(), 348, 150, -793);
	}

	public static void removeWallPlayer(Player player)
	{
		Brave.wallPlayers.remove(player.getName());		
	}

	public static Collection<WallPlayer> getWallPlayerList()
	{
		return Brave.wallPlayers.values();
	}
}